﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
   public class NguoiDungDAO
    {
       public List<NguoiDungDTO> GetAllNguoiDung()
       {
           string sql = "select * from NguoiDung";
           SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
           SqlCommand cmd = new SqlCommand(sql, conn);
           SqlDataReader reader = cmd.ExecuteReader();
           List<NguoiDungDTO> ds = new List<NguoiDungDTO>();
           while (reader.Read())
           {
               NguoiDungDTO nd = new NguoiDungDTO();
               nd.TenNguoiDung = reader.GetString(0);
               nd.MatKhau = reader.GetString(1);
               nd.HoTen = reader.GetString(2);
               nd.Quyen = reader.GetString(3);
               ds.Add(nd);
           }
           conn.Close();
           return ds;
       }
       public int CapNhat(NguoiDungDTO cn, string sql)
       {
           SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
           SqlCommand cmd = new SqlCommand(sql, conn);
           int res = -1;
           res = cmd.ExecuteNonQuery();
           conn.Close();
           return res;
       }
       public bool KTTenNguoiDung(string userName)
       {
           string sql = "select * from NguoiDung where TenNguoiDung=N'" + userName +"'";
           SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
           SqlCommand cmd = new SqlCommand(sql, conn);
           SqlDataReader reader = cmd.ExecuteReader();
           while (reader.Read())
           {
               return true;
           }
           conn.Close();
           return false;

       }
       public bool KTDoiMatKhau(string userName,string pass)
       {
           string sql = "select * from NguoiDung where TenNguoiDung=N'" + userName + "' and MatKhau=N'"+pass+"'";
           SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
           SqlCommand cmd = new SqlCommand(sql, conn);
           SqlDataReader reader = cmd.ExecuteReader();
           while (reader.Read())
           {
               return true;
           }
           conn.Close();
           return false;

       }

    }
}
